package com.example.labapp;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class selectExamActivity extends AppCompatActivity {
    ListView examList=null;
    AlertDialog loadDialog=null;
    String userID;
    examItemAdapter examItenAdapter_=new examItemAdapter(selectExamActivity.this);

    int passGrade;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_exam);
        examList=findViewById(R.id.examList);
        examList.setAdapter(examItenAdapter_);
        examList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent =new Intent(selectExamActivity.this,examAct.class);
                examItemAdapter.examItemData currentItem= examItenAdapter_.getItemTitle(i);
                if(currentItem.Num>=5 || currentItem.Grade>=passGrade)
                    return;
                intent.putExtra("stdID",userID);
                intent.putExtra("Exam",currentItem.title);
                intent.putExtra("Grade",currentItem.Grade);
                startActivity(intent);
                finish();
            }
        });
        Intent intent=getIntent();
        userID=intent.getStringExtra("stdID");
        loadData();
    }

    AfterThread callbackDB=(ErrorInf res)->
    {
        Looper.prepare();
        loadDialog.cancel();
        if(!res.status)
        {
            Toast.makeText(selectExamActivity.this,"数据加载失败，请重新进入",Toast.LENGTH_SHORT).show();
            finish();
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                examItenAdapter_.updateUI();
            }
        });
        Looper.loop();
    };

    void loadData()
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(selectExamActivity.this);
        builder.setCancelable(false);
        builder.setTitle("下载数据");
        builder.setMessage("正在获取数据");
        loadDialog=builder.create();
        loadDialog.show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                ErrorInf resData=new ErrorInf();
                try {
                    Class.forName("org.mariadb.jdbc.Driver");
                    Connection DBconn = DriverManager.getConnection("jdbc:mariadb://124.222.159.110:3306/laboratory_APP?characterEncoding=utf-8/","labApp","3A9PvxE");
                    Statement statement1= DBconn.createStatement();
                    ResultSet resExamList=statement1.executeQuery(String.format("select ExamName,passGrade from appExams WHERE SubCollege IN (SELECT StdSubCollege FROM StudUser WHERE StdID=%s);",userID));
                    while(resExamList.next())
                    {
                        passGrade=resExamList.getInt("passGrade");
                        examItemAdapter.examItemData newItem=new examItemAdapter.examItemData();
                        newItem.title=resExamList.getString("ExamName");
                        ResultSet resExamInf=statement1.executeQuery("select "+newItem.title+" from StudUser WHERE StdID="+userID+" ;");
                        resExamInf.next();
                        newItem.Grade=resExamInf.getInt(1);
                        resExamInf=statement1.executeQuery(String.format("select %s from examAnswerNum WHERE stdID=%s ;",newItem.title,userID));
                        resExamInf.next();
                        newItem.Num=resExamInf.getInt(1);
                        examItenAdapter_.AddItem(newItem);
                    }
                    DBconn.close();
                } catch (SQLException | ClassNotFoundException throwables) {
                    throwables.printStackTrace();
                    resData.errStr=throwables.getMessage();
                    resData.errCode=1;
                    resData.status=false;
                    callbackDB.callback(resData);
                    return;
                }
                resData.status=true;
                callbackDB.callback(resData);
                return;
            }
        }).start();
    }
}